//
// Created by root on 6/22/17.
//

#include "editor.h"
#include <glib.h>           /* g_fprintf */
#include <glib/gprintf.h>

/* set default values for application & initialize variables */
static void context_set_defaults (FunnyWindow *app) {
    app->window         = NULL;     /* initialize struct values */
    app->winwidth       = 720;      /* default window width  */
    app->winheight      = 740;      /* default window height */
    app->winrestore     = FALSE;    /* restore window size */

    app->view           = NULL;     /* text_view widget for app */
    app->menubar        = NULL;     /* menubar widget */
    app->toolbar        = NULL;     /* toolbar widget */
    app->showtoolbar    = TRUE;     /* toolbar is visible */
    app->statusbar      = NULL;     /* statusbar widget */
    app->cid            = 0;        /* kwinst id for statusbar */
    app->tagtable       = NULL;     /* tagtable for text_view */
    app->tabstop        = 8;        /* number of spaces per tab */
    app->softtab        = 4;        /* soft tab stop size */
    app->tabstring      = NULL;     /* tabstring for indent */
    app->expandtab      = TRUE;     /* insert spaces for tab */
    app->smartbs        = TRUE;     /* use smart backspace */
    app->smarthe        = TRUE;     /* smart home & end cursor */
    app->kphome         = FALSE;    /* previous key press was home */
    app->showtabs       = FALSE;    /* display tabulator markers */
    app->dynwrap        = TRUE;     /* use dynamic word wrap */
    app->showdwrap      = FALSE;    /* use dynamic word wrap */
    app->wraptxtcsr     = TRUE;     /* wrap cursor to next line */
    app->pgudmvscsr     = TRUE;     /* PgUp/PgDn keys move cursor */
    app->indentwspc     = TRUE;     /* indent w/spaces not tabs */
    app->indentmixd     = FALSE;    /* Emacs mode indent w/mixed spc/tabs */
    app->indentauto     = TRUE;     /* auto-indent on return */
    app->posixeof       = TRUE;     /* insure POSIX end of line on save */
    app->trimendws      = TRUE;     /* (bug with Quit) remove trailing ws on save */
#ifdef HAVESOURCEVIEW
    app->langmgr        = gtk_source_language_manager_get_default();
    app->language       = NULL;
//     app->schememgr      = gtk_source_style_scheme_manager_get_default();
//     app->schemeids      = gtk_source_style_scheme_manager_get_scheme_ids (app->schememgr);
    app->highlight      = TRUE;     /* show syntax highlight */
    app->lineno         = FALSE;    /* show line numbers (sourceview) */
    app->linehghlt      = TRUE;     /* highlight current line */
    app->laststyle      = NULL;
#endif
    app->filename       = NULL;     /* full filename */
    app->fname          = NULL;     /* base filename w/o ext */
    app->fext           = NULL;     /* filename extension */
    app->fpath          = NULL;     /* path components of fname */
    app->modified       = FALSE;    /* need save if not 0  */
    app->fsize          = 0;        /* resulting file size */
    app->filemode       = 0;        /* existing file mode */
    app->fileuid        = 0;        /* existing file UID */
    app->filegid        = 0;        /* existing file GID */
    /* initial font name */
    app->fontname       = g_strdup ("monospace 8");

    app->appname        = "Funny Editor";   /* app name, e.g. "GTKwrite Text Editor" */
    app->appshort       = "Funny";  /* short name, e.g. "GTKwrite" */
    app->comment        = g_strdup ("// ");

    app->new_pos        = NULL;     /* Goto mark, no sep init */

    app->printsettings  = NULL;     /* initialize print settings */
    app->printpgsetup   = NULL;     /* initialize page setup */
    app->margintop      = 0.5;      /* default margins */
    app->marginbottom   = 0.5;
    app->marginleft     = 1.0;
    app->marginright    = 0.5;

    app->cfgdir         = NULL;     /* user config dir */
    app->cfgfile        = NULL;     /* user config file */
    app->keyfile        = NULL;
}

void context_init (FunnyWindow *app) {
    /* load default values */
    context_set_defaults (app);
}

int editor_start(int argc, char **argv) {
    FunnyWindow *app = NULL;            /* replaced GtkWidget *window */
    app = g_slice_new (FunnyWindow);    /* allocate mem for struct    */
    context_init (app);                 /* initialize struct values   */

    // gtk_init (&argc, &argv);        /* terminates if can't initialize */
    if (!gtk_init_check (&argc, &argv)) {   /* same but doesn't exit */
        g_fprintf (stderr, "error: gtk_init failed.\n");
        return 1;
    }

    /*
    if (!create_window (app)) {     // create the main window
        g_fprintf (stderr, "error: create_window failed.\n");
        return 1;
    }

    if (argc > 1) {  // TODO: implement getopt
        app->filename = g_strdup (argv[1]);
        split_fname (app);
        buffer_insert_file (app, NULL);
    }
    */

    gtk_main ();

    context_destroy (app);          /* free allocated members   */
    g_slice_free (FunnyWindow, app);    /* free struct memory       */

    return 0;
}

int main(int argc, char **argv) {
    return editor_start(argc, argv);
}

